//
//  TFSetButtonView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TFSetButtonView: UIScrollView {
    @objc weak var delegate2 : TFSetButtonViewDelegate?
    @objc let nameLabArr = NSMutableArray()
    @objc let swicthArr = NSMutableArray()
    @objc var xid:String = ""
    let setNameArr = NSMutableArray()
    let view = UIView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setNameArr.add("摄像头")
        self.setNameArr.add("前置")
        self.setNameArr.add("镜像")
        self.setNameArr.add("美颜")
        self.setNameArr.add("麦克风")
        self.setNameArr.add("扬声器")
        self.setNameArr.add("弹幕")
        self.setNameArr.add("中文")
      
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    @objc func initUI(frame:CGRect,xid:String)
    {
        
        self.frame = frame
        
        for i in 0..<8 {

            let lab = UILabel()
            let setSwicth = TFSwitch()
            if (frame.size.height==320) {
                if (i<4) {
                  lab.frame = CGRect(x: 0, y: 28+35*i, width: 100, height: 24)
             
                setSwicth.frame = CGRect(x: 120, y: 25+35*i, width: 50, height: 25)
                
                } else {
                  lab.frame = CGRect(x: 0, y: 80+(i-3)*55, width: 100, height: 24)
                
                  setSwicth.frame = CGRect(x: 120, y: 100+(i-3)*55, width: 50, height: 25)
            
                }
            } else {
                if (i<4) {
                  lab.frame = CGRect(x: 0, y:  28+40*i, width: 100, height: 24)
              
                  setSwicth.frame = CGRect(x: 120, y: 25+40*i, width: 50, height: 25)
                   
                } else {
                  lab.frame = CGRect(x: 0, y: 145+(i-3)*55, width: 100, height: 24)
              
                  setSwicth.frame = CGRect(x: 120, y: 145+(i-3)*55, width: 50, height: 25)
                  
                }
            }
          
            lab.textAlignment = .right
            
            lab.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
            lab.font = UIFont.PingFang(size: 17)
            self.addSubview(lab)

            let attributes1 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)]
            
            let attributes2 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)]
            
            if (i==0) {
                /**获取 小班摄像头 开关 状态*/
                lab.text = TFChangeLanguage.localized("camera")
                
                setSwicth.isOn = TalkfunSettingToolsManager.shared().getToolsSwitchVideo(xid)
                
            } else if (i==1) {
                /**获取 小班前置 开关 状态*/
                setSwicth.isOn = !TalkfunSettingToolsManager.shared().getToolsSwitchCamera(xid)
                
                let str = TFChangeLanguage.localized("front_camera")
                
                let string = NSMutableAttributedString.getAttributed(string: str, font: UIFont.PingFang(size: 17), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1))
                
                if (setSwicth.isOn) {
            
                    string.addAttributes(attributes1, range: NSRange(location: 0, length: 2))
                    string.addAttributes(attributes2, range: NSRange(location: 2, length: 3))
                   
                } else {
                    string.addAttributes(attributes2, range: NSRange(location: 0, length: 2))
                    
              
                    string.addAttributes(attributes1, range: NSRange(location: 2, length: 3))
                   
                }
                lab.attributedText = string
            } else if (i==2) {
                //获取 小班镜像 开关 状态
                lab.text = TFChangeLanguage.localized("mirror")
                
                let Mirror:Bool =  TalkfunSettingToolsManager.shared().getToolsSwitchMirror(xid)
                setSwicth.isOn = !Mirror
            } else if (i==3) {
                
                lab.text = TFChangeLanguage.localized("meiyan")
                /**获取 小班美颜 开关 状态*/
                let Beauty:Bool = TalkfunSettingToolsManager.shared().getToolsSwitchBeauty(xid)
                setSwicth.isOn = Beauty
            } else if (i==4){
                
                lab.text = TFChangeLanguage.localized("microphone")
                /**获取 小班麦克风 开关 状态*/
                setSwicth.isOn = TalkfunSettingToolsManager.shared().getToolsSwitchAudio(xid)
                
//                print("小班麦克风 开关 状态===>%i", setSwicth.isOn)
            } else if (i==5) {
                
                lab.text = TFChangeLanguage.localized("speaker")
                
                /**获取 小班扬声器 开关 状态*/
                setSwicth.isOn = TalkfunSettingToolsManager.shared().getToolsSwitchSpeaker(xid)
            } else if (i==6) {
                /**获取 小班弹幕 开关 状态*/
                lab.text = TFChangeLanguage.localized("barrage")
                
                setSwicth.isOn = TalkfunSettingToolsManager.shared().getToolsSwitchBarrage(xid)
              
            } else if (i==7) {
                self.upUI(lab7: lab, setSwicth7: setSwicth)
              
            }
           
            setSwicth.onTintColor =  UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1)
            setSwicth.tintColor =  UIColor(red: 76/255.0, green: 81/255.0, blue: 88/255.0, alpha: 1)
            setSwicth.switchBtn.tag = i
            setSwicth.switchBtn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            self.addSubview(setSwicth)
          
            if (self.swicthArr.count<8) {
       
                self.swicthArr.add(setSwicth)
                self.nameLabArr.add(lab)
            }
        }
        
        self.backgroundColor = UIColor(red: 28/255.0, green: 31/255.0, blue: 35/255.0, alpha: 0.98)
        self.hiddenBtn(true)
    }
    
    @objc func btnClick(_ btn:UIButton)
    {
        let tag = btn.tag
        let setSwicth = self.swicthArr[tag] as! TFSwitch
        
        let isSwitch:Bool = setSwicth.isOn

        if (tag == 0) {

        } else if (tag == 1) {
            
        } else if (tag == 2 || tag == 3 || tag == 5) {
            setSwicth.isOn = !isSwitch
            
        } else if (tag == 4) {
            
        }
        
        if self.delegate2 != nil && (self.delegate2?.responds(to: #selector(TFSetButtonViewDelegate.SetUserFunction(_:andSwitch:))))!{
     
               self.delegate2?.SetUserFunction(tag, andSwitch: setSwicth)
           }
    }
    @objc func hiddenBtn(_ isOn:Bool)
    {
        if self.nameLabArr.count == 0 && self.swicthArr.count == 0{
            self.initUI(frame: self.frame, xid: self.xid)
        }
        
        let isOnN = false
        let lab1 = self.nameLabArr[1] as! UILabel
        let lab2 = self.nameLabArr[2] as! UILabel
        let lab3 = self.nameLabArr[3] as! UILabel
        let lab4 = self.nameLabArr[4] as! UILabel
        let lab5 = self.nameLabArr[5] as! UILabel
        let lab6 = self.nameLabArr[6] as! UILabel
        let lab7 = self.nameLabArr[7] as! UILabel
        
        let switch0 = self.swicthArr[0] as! UISwitch
        let switch1 = self.swicthArr[1] as! UISwitch
        let switch2 = self.swicthArr[2] as! UISwitch
        let switch3 = self.swicthArr[3] as! UISwitch
        let switch4 = self.swicthArr[4] as! UISwitch
        let switch5 = self.swicthArr[5] as! UISwitch
        let switch6 = self.swicthArr[6] as! UISwitch
        let switch7 = self.swicthArr[7] as! UISwitch
       
        
        if (self.frame.size.height == 320) {
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                if (isOnN) {
                          
                   lab4.frame = CGRect(x: 0, y: 130+(1*45), width: 100, height: 24)
                   lab5.frame = CGRect(x: 0, y: 130+(2*45), width: 100, height: 24)
                   lab6.frame = CGRect(x: 0, y: 130+(3*45), width: 100, height: 24)
                   lab7.frame = CGRect(x: 0, y: 130+(4*45), width: 100, height: 24)
                    
                    switch4.frame = CGRect(x: 120, y: 130+(1*45), width: 50, height: 25)
                    switch5.frame = CGRect(x: 120, y: 130+(2*45), width: 50, height: 25)
                    switch6.frame = CGRect(x: 120, y: 130+(3*45), width: 50, height: 25)
                    switch7.frame = CGRect(x: 120, y: 130+(4*45), width: 50, height: 25)
                    
                    switch0.isOn = true
                } else {
                            lab1.isHidden = true
                            lab2.isHidden = true
                            lab3.isHidden = true
                    
                            lab4.frame = CGRect(x: 0, y: (54*0)+28+24+30, width: 100, height: 24)
                            lab5.frame = CGRect(x: 0, y: (54*1)+28+24+30, width: 100, height: 24)
                            lab6.frame = CGRect(x: 0, y: (54*2)+28+24+30, width: 100, height: 24)
                            lab7.frame = CGRect(x: 0, y: (54*3)+28+24+30, width: 100, height: 24)
                    
                    
                            switch1.isHidden = true
                            switch2.isHidden = true
                            switch3.isHidden = true
                    
                            switch4.frame = CGRect(x: 120, y: 52+30, width: 50, height: 25)
                            switch5.frame = CGRect(x: 120, y: (54*1)+28+24+30, width: 50, height: 25)
                            switch6.frame = CGRect(x: 120, y: (54*2)+28+24+30, width: 50, height: 25)
                            switch7.frame = CGRect(x: 120, y: (54*3)+28+24+30, width: 50, height: 25)
                            switch0.isOn = false
                }
            } completion: { Bool in
                if (isOnN) {
                    lab1.isHidden = false
                    lab2.isHidden = false
                    
                    lab3.isHidden = false
                    switch1.isHidden = false
                    switch2.isHidden = false
                    switch3.isHidden = false
                }
            }
           
        } else {
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                if (isOnN) {
                
                           lab4.frame = CGRect(x: 0, y: 145+(1*55), width: 100, height: 24)
                           lab5.frame = CGRect(x: 0, y: 145+(2*55), width: 100, height: 24)
                           lab6.frame = CGRect(x: 0, y: 145+(3*55), width: 100, height: 24)
                           lab7.frame = CGRect(x: 0, y: 145+(4*55), width: 100, height: 24)
                    
                           switch4.frame = CGRect(x: 120, y: 145+55, width: 50, height: 25)
                           switch5.frame = CGRect(x: 120, y: 145+2*55, width: 50, height: 25)
                           switch6.frame = CGRect(x: 120, y: 145+3*55, width: 50, height: 25)
                           switch7.frame = CGRect(x: 120, y: 145+4*55, width: 50, height: 25)
                           switch0.isOn = true
                } else {
                            lab7.isHidden = true
                   
                            lab2.isHidden = true
                            lab3.isHidden = true
                            lab1.frame = CGRect(x: 0, y: 52+30, width: 100, height: 24)
                            lab4.frame = CGRect(x: 0, y: 82+24+30, width: 100, height: 24)
                            lab5.frame = CGRect(x: 0, y: 136+24+30, width: 100, height: 24)
                            lab6.frame = CGRect(x: 0, y: 190+24+30, width: 100, height: 24)
//                            lab7.frame = CGRect(x: 0, y:  190+24+30, width: 100, height: 24)
                            switch7.isHidden = true
                            switch2.isHidden = true
                            switch3.isHidden = true
                    
                            switch1.frame = CGRect(x: 120, y: 52+30, width: 50, height: 25)
                            switch4.frame = CGRect(x: 120, y: 82+24+30, width: 50, height: 25)
                            switch5.frame = CGRect(x: 120, y: 136+24+30, width: 50, height: 25)
                            switch6.frame = CGRect(x: 120, y: 190+24+30, width: 50, height: 25)
                            switch0.isOn = false
                }
            } completion: { Bool in
                
                if (isOnN) {
                    lab1.isHidden = false
                    lab2.isHidden = false
                    lab3.isHidden = false
                    switch1.isHidden = false
                    switch2.isHidden = false
                    switch3.isHidden = false
                }
                
            }
        }
     
    }
    
    @objc func upDataLanguage()
    {
        let attributes1 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)]
        
        let attributes2 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)]
        
        let lab0 = self.nameLabArr[0] as! UILabel
        let lab1 = self.nameLabArr[1] as! UILabel
        let lab2 = self.nameLabArr[2] as! UILabel
        let lab3 = self.nameLabArr[3] as! UILabel
        let lab4 = self.nameLabArr[4] as! UILabel
        let lab5 = self.nameLabArr[5] as! UILabel
        let lab6 = self.nameLabArr[6] as! UILabel
        let lab7 = self.nameLabArr[7] as! UILabel
        
        lab0.text = TFChangeLanguage.localized("camera")
        lab2.text = TFChangeLanguage.localized("mirror")
        lab3.text = TFChangeLanguage.localized("meiyan")
        lab4.text = TFChangeLanguage.localized("microphone")
        lab5.text = TFChangeLanguage.localized("speaker")
        lab6.text = TFChangeLanguage.localized("barrage")

        let setSwicth7 = self.swicthArr[7] as! UISwitch
        
        let switches = self.swicthArr[1] as! UISwitch
        
        let str = TFChangeLanguage.localized("front_camera")
        
        let strArr = str.components(separatedBy: "/")
        
        let qianzhistring = NSMutableAttributedString.getAttributed(string: str, font: UIFont.PingFang(size: 17), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1))
        
        if strArr.count>1 {
            
            let  qianzhistr = strArr[0]
            let  houzhiStr = strArr[1]
       
            if (switches.isOn==false) {
                qianzhistring.addAttributes(attributes2, range: NSRange(location: 0, length: qianzhistr.count))
                qianzhistring.addAttributes(attributes1, range: NSRange(location: qianzhistr.count, length: houzhiStr.count+1))
               
              
            } else {
                qianzhistring.addAttributes(attributes1, range: NSRange(location: 0, length: qianzhistr.count+1))
                qianzhistring.addAttributes(attributes2, range: NSRange(location: qianzhistr.count+1, length: houzhiStr.count))
            
            }
            lab1.attributedText = qianzhistring
        }
       
        self.upUI(lab7: lab7, setSwicth7: setSwicth7)
       
    }
    
    func  upUI(lab7:UILabel,setSwicth7:UISwitch)
    {
        let attributes1 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)]
        
        let attributes2 = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont.PingFang(size: 17),NSMutableAttributedString.Key.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)]
        
        let Engstr = TFChangeLanguage.localized("language_chinese")
                  
        let lang = TFChangeLanguage.sharedInstance.currentLanguage()
        //英文
         if (  lang == "en") {
             setSwicth7.isOn = true
         } else {
             setSwicth7.isOn = false
         }
         
         let string7 = NSMutableAttributedString.getAttributed(string: Engstr, font: UIFont.PingFang(size: 17), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 32/255.0, alpha: 1))
         
         if (setSwicth7.isOn) {
             string7.addAttributes(attributes1, range: NSRange(location: 0, length: 3))
             string7.addAttributes(attributes2, range: NSRange(location: 3, length: 2))
            
           
         } else {
             string7.addAttributes(attributes2, range: NSRange(location: 0, length: 2))
             string7.addAttributes(attributes1, range: NSRange(location: 2, length: 3))
         
         }
         
         lab7.attributedText = string7
    }
}


@objc protocol TFSetButtonViewDelegate : NSObjectProtocol {

    func SetUserFunction(_ tag : NSInteger,andSwitch:UISwitch)
  
}


